﻿
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Mes.PlanDetails.Dtos;
using KDS.Mes.Authorization;
using KDS.Mes.Dto;

namespace KDS.Mes.Mes.PlanDetails
{
    /// <summary>
    /// 详细计划信息服务实现
    /// </summary>
    //[AbpAuthorize(AppPermissions.Pages_Plan_PlanDetails)]
    public class PlanDetailAppService : MesAppServiceBase, IPlanDetailAppService
    {
        private readonly IRepository<PlanDetail, int> _planDetailRepository;
        private readonly IPlanDetailListExcelExporter _planDetailListExcelExporter;


        private readonly PlanDetailManage _planDetailManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public PlanDetailAppService(IRepository<PlanDetail, int> planDetailRepository,
PlanDetailManage planDetailManage
      , IPlanDetailListExcelExporter planDetailListExcelExporter
  )
        {
            _planDetailRepository = planDetailRepository;
            _planDetailManage = planDetailManage;
            _planDetailListExcelExporter = planDetailListExcelExporter;
        }

        #region 详细计划信息管理

        public PagedResultDto<PlanDetailListDto> GetPagedPlanDetails(GetPlanDetailInput input)
        {
            //初步过滤
            var query = _planDetailRepository.GetAll().Where(u => u.PlanId == input.PlanId);
            //排序
            //query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            query = query.OrderBy(t => t.SeatSeq).ThenBy(t => t.SeatPositionId);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<PlanDetailListDto>(tasksCount, taskList.MapTo<List<PlanDetailListDto>>());
        }



        /// <summary>
        /// 根据查询条件获取详细计划信息分页列表
        /// </summary>
        public async Task<PagedResultDto<PlanDetailListDto>> GetPagedPlanDetailsAsync(GetPlanDetailInput input)
        {

            var query = _planDetailRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var planDetailCount = await query.CountAsync();

            var planDetails = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var planDetailListDtos = planDetails.MapTo<List<PlanDetailListDto>>();
            return new PagedResultDto<PlanDetailListDto>(
            planDetailCount,
            planDetailListDtos
            );
        }

        /// <summary>
        /// 通过Id获取详细计划信息信息进行编辑或修改 
        /// </summary>
        public async Task<GetPlanDetailForEditOutput> GetPlanDetailForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetPlanDetailForEditOutput();

            PlanDetailEditDto planDetailEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _planDetailRepository.GetAsync(input.Id.Value);
                planDetailEditDto = entity.MapTo<PlanDetailEditDto>();
            }
            else
            {
                planDetailEditDto = new PlanDetailEditDto();
            }

            output.PlanDetail = planDetailEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取详细计划信息ListDto信息
        /// </summary>
        public async Task<PlanDetailListDto> GetPlanDetailByIdAsync(EntityDto<int> input)
        {
            var entity = await _planDetailRepository.GetAsync(input.Id);

            return entity.MapTo<PlanDetailListDto>();
        }







        /// <summary>
        /// 新增或更改详细计划信息
        /// </summary>
        public async Task CreateOrUpdatePlanDetailAsync(CreateOrUpdatePlanDetailInput input)
        {
            if (input.PlanDetailEditDto.Id.HasValue)
            {
                await UpdatePlanDetailAsync(input.PlanDetailEditDto);
            }
            else
            {
                await CreatePlanDetailAsync(input.PlanDetailEditDto);
            }
        }

        /// <summary>
        /// 新增详细计划信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanDetails_Create)]
        public virtual async Task<PlanDetailEditDto> CreatePlanDetailAsync(PlanDetailEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<PlanDetail>();

            entity = await _planDetailRepository.InsertAsync(entity);
            return entity.MapTo<PlanDetailEditDto>();
        }

        /// <summary>
        /// 编辑详细计划信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanDetails_Edit)]
        public virtual async Task UpdatePlanDetailAsync(PlanDetailEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _planDetailRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _planDetailRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除详细计划信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanDetails_Delete)]
        public async Task DeletePlanDetailAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _planDetailRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除详细计划信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Plan_PlanDetails_Delete)]
        public async Task BatchDeletePlanDetailAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _planDetailRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion


        #region 详细计划信息的Excel导出功能


        public async Task<FileDto> GetPlanDetailToExcel()
        {
            var entities = await _planDetailRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<PlanDetailListDto>>();

            var fileDto = _planDetailListExcelExporter.ExportPlanDetailToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
